class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}
public class Solution {
    public ListNode ReverseList(ListNode head) {
        ListNode first = new ListNode(0);

        if (head == null) {
            return null;
        }

        while (head != null) {
            if (first.next == null) {
                first.next = head;
                head = head.next;
                continue;
            }
            ListNode cur = head.next;
            head.next = first.next;
            first.next = head;
            head = cur;
        }

        return first.next;
    }
}
